<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>球队管理</title>
    <link href="/include/css/smoothness/jquery-ui-1.10.3.custom.min.css" rel="stylesheet">
	<script type="text/javascript" src="/include/js/jquery-1.9.1.js"></script>
	<script type="text/javascript" src="/include/js/jquery-ui-1.10.3.custom.min.js"></script>
	<script type="text/javascript" src="/include/js/jquery.ui.datepicker-zh-cn.js"></script>
	<script >
	$(function() {
	    $( "#datepicker" ).datepicker({
	        buttonImage: "images/calendar.gif",
	        buttonImageOnly: true,
	        changeMonth: true,
	        changeYear: true
	    });
	});
	</script>
</head>
<body>
<?php
require dirname(__FILE__) . '/checkuser.php';
require dirname(dirname(__FILE__)) . '/include/conf.php';
require dirname(dirname(__FILE__)) . '/include/database.php';
require dirname(dirname(__FILE__)) . '/include/py.php';

$db = db_touzhu();

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	// 处理球队匹配结果
	$all_teams = $_all_teams = array();
	$subdata = $_POST;
	foreach ($subdata as $key => $v){
		list($_tmp, $team_id) = explode('_', $key);
		$team_id = (int) $team_id;
		if (is_numeric($team_id) && $team_id > 0 && $v !== '0'){
			$all_teams[] = array('id' => $team_id, 'team' => $v);
			$_all_teams[$v] = 0;
		}
	}
	if ((count($_all_teams) != count($all_teams)) || empty($all_teams)){
		exit('没有选择或有重复，请重新选择！<a href="javascript: history.go(-1);">返回</a>');
	}
	
	foreach ($all_teams as $team){
		$sql_team = "update teams set 188bet_id={$team['id']} where `from`=2 and team='{$team['team']}'";
		$sql_match1 = "update ball_disc set home_team_id={$team['id']} where `from`=2 and home_team='{$team['team']}'";
		$sql_match2 = "update ball_disc set visiting_team_id={$team['id']} where `from`=2 and visiting_team='{$team['team']}'";
		$re_team = $db->query($sql_team);
		$re_match1 = $db->query($sql_match1);
		$re_match2 = $db->query($sql_match2);
	}
	$msg = '球队配对' . ($re_team ? '<span style="color: green;">成功</span>' : '<span style="color: red;">失败</span>') . "<br />\n";
	$msg .= '更新比赛球队ID' . (($re_match1 && $re_match2) ? '<span style="color: green;">成功</span>' : '<span style="color: red;">失败</span>') . "<br />\n";
	$msg .= '<br/><a href="javascript: history.go(-1);">返回</a> | <a href="teamlist.php">查看配对结果</a>';
	exit($msg);
}

$pairtime = (isset($_GET['date']) && strtotime($_GET['date'])) ? strtotime($_GET['date']) : time();
if (date('H') < 12){
	$starttime = date('Y-m-d 12:00:00', $pairtime - 86400);
	$endtime = date('Y-m-d 11:59:59', $pairtime);
}else {
	$starttime = date('Y-m-d 12:00:00', $pairtime);
	$endtime = date('Y-m-d 11:59:59', $pairtime + 86400);
}

$sql_teams = "select home_team, visiting_team, home_team_id, visiting_team_id, `from` from ball_disc ";
$sql_teams .= "where fetch_time>='{$starttime}' and fetch_time<='{$endtime}' group by home_team, visiting_team";
$teams_all = $db->query($sql_teams);

if (!empty($teams_all)){
	$teams_365 = $teams_188_paired = $teams_188_nopaired = $teams_188_nopaired_sort = $ids_188 = array();
	foreach ($teams_all as $v){
		if ($v['from'] == 1){
			$teams_365[$v['home_team_id']] = $v['home_team'];
			$teams_365[$v['visiting_team_id']] = $v['visiting_team'];
		}elseif($v['from'] == 2) {
			if ($v['home_team_id'] != 0){
				$ids_188[] = $v['home_team_id'];
				$teams_188_paired[$v['home_team_id']] = $v['home_team'];
			}else {
				$teams_188_nopaired[] = $v['home_team'];
				$teams_188_nopaired_sort[] = getfirstchar($v['home_team']);
			}
			if ($v['visiting_team_id'] != 0){
				$ids_188[] = $v['visiting_team_id'];
				$teams_188_paired[$v['visiting_team_id']] = $v['visiting_team'];
			}else {
				$teams_188_nopaired[] = $v['visiting_team'];
				$teams_188_nopaired_sort[] = getfirstchar($v['visiting_team']);
			}
		}
	}
	
	// 显示已经配对的
	if (!empty($teams_188_paired)){
		echo '<strong>已配对(当天)：</strong><br/>';
		foreach ($teams_188_paired as $_key => $v){
			if (isset($teams_365[$_key])){
				$_team365 = $teams_365[$_key];
				// 已经配对的在数组中删除
				//unset($teams_365[$_key]);
			}else {
				$_team365 = '';
			}
			echo "{$_team365} - {$v}<br/>\n";
		}
	}
}
?>
<br/>
<form action="" method="get">
日期：
<input type="text" name="date" id="datepicker" value="<?php echo date('Y-m-d', $pairtime);?>" />
<input type="submit" value=" GO " />
</form>
<hr></hr>
<form action="" method="POST">
<table border="1" cellpadding="5" cellspacing="0">
	<col align="left" width="190" />
	<col align="left" width="190" />
	<tr>
		<th>bet365</th><th>188bet</th>
	</tr>
<?php
if (!empty($teams_365) && !empty($teams_188_nopaired)){
	// 按拼音排序
	array_multisort($teams_188_nopaired_sort, $teams_188_nopaired);
	
	$option_html = '<option value="0">选择球队</option>';
	foreach ($teams_188_nopaired as $team){
		$option_html .= "<option value=\"{$team}\">{$team}</option>\n";
	}

	foreach ($teams_365 as $_key => $team){
		if (!empty($team)){
			echo "<tr><td>{$team}</td><td><select name=\"team_{$_key}\">",
				$option_html, '</select></td></tr>';
		}
	}
}
?>
</table>
<br />
<input type="submit" value=" 保 存 " />
</form>

</body>
</html>